<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"/>
    <title>后台管理系统</title>
    <link rel="icon" href="../static/favicon.ico" type="image/ico">
    <meta name="keywords" content="后台管理系统">
    <meta name="description" content="后台管理系统(Book Manage System)">
    <meta name="author" content="拾叁柒">
    <link href="../static/lyear-template/css/bootstrap.min.css" rel="stylesheet">
    <link href="../static/lyear-template/css/materialdesignicons.min.css" rel="stylesheet">
    <link href="../static/lyear-template/css/style.min.css" rel="stylesheet">
    <link href="../static/lyear-template/js/jconfirm/jquery-confirm.min.css" rel="stylesheet">
</head>

<body>
<div class="lyear-layout-web" id="app">
    <div class="lyear-layout-container">
        <!--左侧导航-->
        <aside class="lyear-layout-sidebar">

            <!-- logo -->
            <div id="logo" class="sidebar-header">
                <a href="user.html"><img src="../static/lyear-template/images/logo-sidebar.png" title="后台管理系统"
                                         alt="后台管理系统"/></a>
            </div>
            <div class="lyear-layout-sidebar-scroll">

                <nav class="sidebar-main">
                    <ul class="nav nav-drawer">
                        <li class="nav-item"><a href="index.html"><i class="mdi mdi-home"></i> 后台首页</a></li>
                        <!--用户管理-->
                        <li class="nav-item nav-item-has-subnav active open">
                            <a href="javascript:void(0)"><i class="mdi mdi-account" target="_blank"></i>用户管理</a>
                            <ul class="nav nav-subnav">
                                <li><a href="user.html" target="_blank">用户列表</a></li>
                            </ul>
                        </li>
                        <!--图书管理-->
                        <li class="nav-item nav-item-has-subnav">
                            <a href="javascript:void(0)"><i class="mdi mdi-format-align-justify" target="_blank"></i>图书管理</a>
                            <ul class="nav nav-subnav">
                                <li><a href="add.html" target="_blank">添加图书</a></li>
                                <li><a href="book.html" target="_blank">图书列表</a></li>
                            </ul>
                        </li>
                    </ul>
                </nav>

                <div class="sidebar-footer">
                    <p class="copyright">Copyright &copy; 2022. <a target="_blank"
                                                                   href="http://www.ihtry.com">拾叁柒</a> All
                        rights
                        reserved.</p>
                </div>
            </div>

        </aside>
        <!--End 左侧导航-->

        <!--头部信息-->
        <header class="lyear-layout-header">

            <nav class="navbar navbar-default">
                <div class="topbar">

                    <div class="topbar-left">
                        <div class="lyear-aside-toggler">
                            <span class="lyear-toggler-bar"></span>
                            <span class="lyear-toggler-bar"></span>
                            <span class="lyear-toggler-bar"></span>
                        </div>
                        <span class="navbar-page-title">图书管理</span>
                    </div>

                    <ul class="topbar-right">
                        <li class="dropdown dropdown-profile">
                            <a href="javascript:void(0)" data-toggle="dropdown">
                                <img class="img-avatar img-avatar-48 m-r-10"
                                     src="../static/lyear-template/images/avater.jpg" alt="拾叁柒"/>
                                <span>拾叁柒<span class="caret"></span></span>
                            </a>
                            <ul class="dropdown-menu dropdown-menu-right">
                                <li><a href="/login.html"><i class="mdi mdi-logout-variant"></i> 退出登录</a>
                                </li>
                            </ul>
                        </li>
                        <!--切换主题配色-->
                        <li class="dropdown dropdown-skin">
                            <span data-toggle="dropdown" class="icon-palette"><i class="mdi mdi-palette"></i></span>
                            <ul class="dropdown-menu dropdown-menu-right" data-stopPropagation="true">
                                <li class="drop-title"><p>主题</p></li>
                                <li class="drop-skin-li clearfix">
                  <span class="inverse">
                    <input type="radio" name="site_theme" value="default" id="site_theme_1" checked>
                    <label for="site_theme_1"></label>
                  </span>
                                    <span>
                    <input type="radio" name="site_theme" value="dark" id="site_theme_2">
                    <label for="site_theme_2"></label>
                  </span>
                                    <span>
                    <input type="radio" name="site_theme" value="translucent" id="site_theme_3">
                    <label for="site_theme_3"></label>
                  </span>
                                </li>
                                <li class="drop-title"><p>LOGO</p></li>
                                <li class="drop-skin-li clearfix">
                  <span class="inverse">
                    <input type="radio" name="logo_bg" value="default" id="logo_bg_1" checked>
                    <label for="logo_bg_1"></label>
                  </span>
                                    <span>
                    <input type="radio" name="logo_bg" value="color_2" id="logo_bg_2">
                    <label for="logo_bg_2"></label>
                  </span>
                                    <span>
                    <input type="radio" name="logo_bg" value="color_3" id="logo_bg_3">
                    <label for="logo_bg_3"></label>
                  </span>
                                    <span>
                    <input type="radio" name="logo_bg" value="color_4" id="logo_bg_4">
                    <label for="logo_bg_4"></label>
                  </span>
                                    <span>
                    <input type="radio" name="logo_bg" value="color_5" id="logo_bg_5">
                    <label for="logo_bg_5"></label>
                  </span>
                                    <span>
                    <input type="radio" name="logo_bg" value="color_6" id="logo_bg_6">
                    <label for="logo_bg_6"></label>
                  </span>
                                    <span>
                    <input type="radio" name="logo_bg" value="color_7" id="logo_bg_7">
                    <label for="logo_bg_7"></label>
                  </span>
                                    <span>
                    <input type="radio" name="logo_bg" value="color_8" id="logo_bg_8">
                    <label for="logo_bg_8"></label>
                  </span>
                                </li>
                                <li class="drop-title"><p>头部</p></li>
                                <li class="drop-skin-li clearfix">
                  <span class="inverse">
                    <input type="radio" name="header_bg" value="default" id="header_bg_1" checked>
                    <label for="header_bg_1"></label>
                  </span>
                                    <span>
                    <input type="radio" name="header_bg" value="color_2" id="header_bg_2">
                    <label for="header_bg_2"></label>
                  </span>
                                    <span>
                    <input type="radio" name="header_bg" value="color_3" id="header_bg_3">
                    <label for="header_bg_3"></label>
                  </span>
                                    <span>
                    <input type="radio" name="header_bg" value="color_4" id="header_bg_4">
                    <label for="header_bg_4"></label>
                  </span>
                                    <span>
                    <input type="radio" name="header_bg" value="color_5" id="header_bg_5">
                    <label for="header_bg_5"></label>
                  </span>
                                    <span>
                    <input type="radio" name="header_bg" value="color_6" id="header_bg_6">
                    <label for="header_bg_6"></label>
                  </span>
                                    <span>
                    <input type="radio" name="header_bg" value="color_7" id="header_bg_7">
                    <label for="header_bg_7"></label>
                  </span>
                                    <span>
                    <input type="radio" name="header_bg" value="color_8" id="header_bg_8">
                    <label for="header_bg_8"></label>
                  </span>
                                </li>
                                <li class="drop-title"><p>侧边栏</p></li>
                                <li class="drop-skin-li clearfix">
                  <span class="inverse">
                    <input type="radio" name="sidebar_bg" value="default" id="sidebar_bg_1" checked>
                    <label for="sidebar_bg_1"></label>
                  </span>
                                    <span>
                    <input type="radio" name="sidebar_bg" value="color_2" id="sidebar_bg_2">
                    <label for="sidebar_bg_2"></label>
                  </span>
                                    <span>
                    <input type="radio" name="sidebar_bg" value="color_3" id="sidebar_bg_3">
                    <label for="sidebar_bg_3"></label>
                  </span>
                                    <span>
                    <input type="radio" name="sidebar_bg" value="color_4" id="sidebar_bg_4">
                    <label for="sidebar_bg_4"></label>
                  </span>
                                    <span>
                    <input type="radio" name="sidebar_bg" value="color_5" id="sidebar_bg_5">
                    <label for="sidebar_bg_5"></label>
                  </span>
                                    <span>
                    <input type="radio" name="sidebar_bg" value="color_6" id="sidebar_bg_6">
                    <label for="sidebar_bg_6"></label>
                  </span>
                                    <span>
                    <input type="radio" name="sidebar_bg" value="color_7" id="sidebar_bg_7">
                    <label for="sidebar_bg_7"></label>
                  </span>
                                    <span>
                    <input type="radio" name="sidebar_bg" value="color_8" id="sidebar_bg_8">
                    <label for="sidebar_bg_8"></label>
                  </span>
                                </li>
                            </ul>
                        </li>
                        <!--切换主题配色-->
                    </ul>

                </div>
            </nav>

        </header>
        <!--End 头部信息-->

        <!--页面主要内容-->
        <main class="lyear-layout-content">

            <div class="container-fluid">

                <div class="row">
                    <div class="col-lg-12">
                        <div class="card">
                            <div class="card-header"><h4>图书列表</h4></div>
                            <div class="card-toolbar clearfix">
                                <div class="toolbar-btn-action">
                                    <a class="btn btn-primary m-r-5" href="add.html">
                                        <i class="mdi mdi-plus"></i> 新增
                                    </a>
                                </div>
                                <div class="card-body">
                                    <div class="table-responsive">
                                        <table class="table table-hover table-bordered">
                                            <thead>
                                            <tr>
                                                <th>#</th>
                                                <th>书名</th>
                                                <th>作者</th>
                                                <th>价格</th>
                                                <th>数量</th>
                                                <th>操作</th>
                                            </tr>
                                            </thead>
                                            <tbody>
                                            <tr v-for="(item,index) in dataList" :key="item.id">
                                                <th scope="row">{{index+1}}</th>
                                                <td>{{item.name}}</td>
                                                <td>{{item.author}}</td>
                                                <td>{{item.price}}</td>
                                                <td>{{item.number}}</td>
                                                <td>
                                                    <button class="btn btn-info btn-xs" data-toggle="modal"
                                                            data-target="#myModal" @click="openEditDialog(item.id)">编辑
                                                    </button>
                                                    <button class="btn btn-danger btn-xs" @click="deleteBook(item.id);">
                                                        删除
                                                    </button>
                                                </td>
                                            </tr>
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <!--编辑用户弹窗-->
                    <div class="editDialog">
                        <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
                            <div class="modal-dialog" role="document">
                                <div class="modal-content">
                                    <div class="modal-header">
                                        <button type="button" class="close" data-dismiss="modal"
                                                aria-label="Close"><span
                                                aria-hidden="true">&times;</span></button>
                                        <h4 class="modal-title" id="myModalLabel">编辑图书</h4>
                                    </div>
                                    <div class="modal-body">
                                        <div class="edit-table">
                                            <div class="form-group">
                                                <label for="name">书名</label>
                                                <input class="form-control" type="text" id="name"
                                                       name="name" v-model="formData.name">
                                            </div>
                                            <div class="form-group">
                                                <label for="author">作者</label>
                                                <input class="form-control" type="text" id="author"
                                                       name="author" v-model="formData.author">
                                            </div>
                                            <div class="form-group">
                                                <label for="price">价格</label>
                                                <input class="form-control" type="text" id="price"
                                                       name="price" v-model="formData.price">
                                            </div>
                                            <div class="form-group">
                                                <label for="number">数量</label>
                                                <input class="form-control" type="text" id="number"
                                                       name="number" v-model="formData.number">
                                            </div>
                                        </div>

                                    </div>
                                    <div class="modal-footer">
                                        <button type="button" class="btn btn-default" data-dismiss="modal"
                                                @click="resetForm();">取消
                                        </button>
                                        <button type="button" class="btn btn-primary"
                                                @click="editBook();">修改
                                        </button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                </div>

        </main>
        <!--End 页面主要内容-->
    </div>
</div>

<script type="text/javascript" src="../static/jquery.min.js"></script>
<script type="text/javascript" src="../static/lyear-template/js/bootstrap.min.js"></script>
<script type="text/javascript" src="../static/lyear-template/js/perfect-scrollbar.min.js"></script>
<script type="text/javascript" src="../static/lyear-template/js/main.min.js"></script>
<script src="../static/vue.js"></script>
<script src="../static/axios-0.18.0.js"></script>
<script src="../static/lyear-template/js/bootstrap-notify.min.js"></script>
<script src="../static/lyear-template/js/lightyear.js"></script>
<script src="../static/lyear-template/js/jconfirm/jquery-confirm.min.js"></script>
<script>
    var app = new Vue({
        el: "#app",
        data: {
            dataList: [],
            formData: {
                name: "",
                author: "",
                price: "",
                number: ""
            }

        },
        created() {
            //初始化执行
            this.getAll();
        },
        methods: {
            //获取全部
            getAll() {
                axios.get("/books").then((res) => {
                    this.dataList = res.data.data;
                });

            },
            resetForm() {
                this.formData = {};
            },

            //打开编辑窗口
            openEditDialog(id) {
                axios.get("/books/" + id).then((res) => {
                    this.formData = res.data.data;
                })

            },
            //修改按钮提交
            editBook() {
                if (this.formData.name == "") {
                    lightyear.notify("书名不能为空!", "warning", 3000);
                } else if (this.formData.author == "") {
                    lightyear.notify("作者不能为空!", "warning", 3000);
                } else if (this.formData.price == "") {
                    lightyear.notify("价格不能为空!", "warning", 3000);
                } else if (this.formData.number == "") {
                    lightyear.notify("数量不能为空!", "warning", 3000);
                } else {
                    axios.put("/books", this.formData).then((res) => {
                        if (res.data.code === 20040) {
                            lightyear.notify(res.data.msg, 'success', 1000);
                            //关闭窗口
                            $("#myModal").modal("hide");
                        } else {
                            lightyear.notify(res.data.msg, 'danger', 1000);
                        }
                    }).finally(() => {
                        this.getAll();
                    });
                }


            },

            //删除图书
            deleteBook(id) {
                $.confirm({
                    title: '操作提示',
                    content: "您是否要删除该选项?",
                    type: 'orange',
                    typeAnimated: false,
                    buttons: {
                        omg: {
                            text: '删除',
                            btnClass: 'btn-orange',
                            action: () => {
                                axios.delete("/books/" + id).then((res) => {
                                    if (res.data.code === 20030) {
                                        lightyear.notify(res.data.msg, 'success', 1000);
                                        this.getAll();
                                    } else {
                                        lightyear.notify(res.data.msg, 'danger', 1000);
                                    }
                                });

                            }

                        },
                        close: {
                            text: '取消',
                        }


                    }
                });
            }

        }

    });

</script>
</body>
</html>